package ui.activity.mine;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.*;
import base.BaseBean;
import base.activity.BaseLayoutActivity;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.dispatchapp.andlei.R;
import netWork.NetUtils;
import widget.ShopStatusPopupWindow;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 身份认证
 *
 * @author Andlei
 * @DATE 2019/1/23
 */
public class IdentityActivity extends BaseLayoutActivity {
    private RadioGroup radioGroup;
    private RadioButton rbtn_zai, rbtn_fei;
    private TextView tv_sex, tv_schoolname, tv_grade, tv_photo;
    private EditText et_name, et_card, et_major, et_student_number;
    private LinearLayout layout_major, layout_grade, layout_student_number;
    private RelativeLayout rlayout_shenhezhong;
    private String school_id;
    private TextView tv_tongguo;
    private NetUtils netUtils;
    private TimePickerView pvCustomLunar;
    private PopupWindow pop;
    private String is_card;
    private String img1, img2;
    private View mView;
    private TextView title_base_right;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("身份认证", true);
        setTitlefontBg_black();
        title_base_right = setBaseRightText("提交", Color.parseColor("#00B89D"));
        //1审核通过 0审核未通过 2审核中
        //是否审核通过，0未认证，1审核中，2审核通过，3未通过
        if(is_card.equals("1")){
            rlayout_shenhezhong.setVisibility(View.VISIBLE);
            title_base_right.setVisibility(View.GONE);
        }else if(is_card.equals("2")){
            rlayout_shenhezhong.setVisibility(View.GONE);
            tv_tongguo.setVisibility(View.VISIBLE);
            title_base_right.setVisibility(View.GONE);
        }else {
            rlayout_shenhezhong.setVisibility(View.GONE);
            tv_tongguo.setVisibility(View.GONE);
            title_base_right.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected int getLayoutId() {
        mView = getWindow().getDecorView();
        return R.layout.activity_identity;
    }

    @Override
    protected void findViews() {
        netUtils = new NetUtils(mActivity, netRequestCallBack);
        rlayout_shenhezhong = getView(R.id.rlayout_shenhezhong);
        radioGroup = getView(R.id.rg_);
        rbtn_zai = getView(R.id.rbtn_zai);
        rbtn_fei = getView(R.id.rbtn_fei);
        tv_sex = getView(R.id.tv_sex);
        tv_schoolname = getView(R.id.tv_schoolname);
        tv_grade = getView(R.id.tv_grade);
        tv_photo = getView(R.id.tv_photo);
        et_name = getView(R.id.et_name);
        et_card = getView(R.id.et_card);
        et_major = getView(R.id.et_major);
        et_student_number = getView(R.id.et_student_number);
        layout_major = getView(R.id.layout_major);
        layout_grade = getView(R.id.layout_grade);
        layout_student_number = getView(R.id.layout_student_number);
        tv_tongguo = getView(R.id.tv_tongguo);
    }
    @Override
    protected void formatViews() {
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rbtn_zai:
                        layout_grade.setVisibility(View.VISIBLE);
                        layout_major.setVisibility(View.VISIBLE);
                        layout_student_number.setVisibility(View.VISIBLE);
                        break;
                    case R.id.rbtn_fei:
                        layout_grade.setVisibility(View.GONE);
                        layout_major.setVisibility(View.GONE);
                        layout_student_number.setVisibility(View.GONE);
                        break;
                }
            }
        });
        tv_grade.setOnClickListener(this);
        tv_photo.setOnClickListener(this);
        tv_sex.setOnClickListener(this);
        tv_schoolname.setOnClickListener(this);

    }
    @Override
    protected void formatData() {

    }

    @Override
    protected void getBundle(Bundle bundle) {
        //1审核通过 0审核未通过 2审核中
        is_card = bundle.getString("is_card");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_right_text:
                //toast("提交");
                Map<String,Object> map = new HashMap<>();
                if(rbtn_zai.isChecked()){
                    map.put("is_student","1");
                }else {
                    map.put("is_student","0");
                }
                map.put("nickname",""+et_name.getText());
                if(TextUtils.isEmpty(tv_sex.getText())){toast("请选择性别！"); return;}
                if(tv_sex.getText().equals("男")){
                    map.put("sex","1");
                }else {
                    map.put("sex","2");
                }
                map.put("id_number",""+et_card.getText());
                if(TextUtils.isEmpty(school_id)){toast("请选择学校"); return;}
                map.put("school_id",school_id);
                map.put("school_name",""+tv_schoolname.getText());
                map.put("major_name",""+et_major.getText());
                map.put("grade",""+tv_grade.getText());
                map.put("student_no",""+et_student_number.getText());
                map.put("face_img",img1);
                map.put("card_img",img2);
                netUtils.post("rider/user/authenticate",map,BaseBean.class,true);
                break;
            case R.id.tv_grade:
                if (pvCustomLunar == null) {
                    initLunarPicker();
                    pvCustomLunar.show(v);
                } else {
                    pvCustomLunar.show(v);
                }
                break;
            case R.id.tv_photo:
                jumpTo(IdentityImageActivty.class,1);
                break;
            case R.id.tv_sex:
                if(pop == null){
                    pop = new ShopStatusPopupWindow(mActivity,"男","女", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if(view.getId() == R.id.tv_open){
                                tv_sex.setText("男");
                            }else {
                                tv_sex.setText("女");
                            }
                            pop.dismiss();
                        }
                    }, false);
                    pop.showAtLocation(mView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL,0,0);
                }else {
                    pop.showAtLocation(mView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL,0,0);
                }
                break;
            case R.id.tv_schoolname:
                jumpTo(SelectSchoolActivity.class,0);
                break;
        }
    }

    private NetUtils.NetRequestCallBack netRequestCallBack = new NetUtils.NetRequestCallBack() {
        @Override
        public void error(String action, Throwable e, Map tag) {

        }

        @Override
        public void success(String action, BaseBean t, Map tag) {
                if(t.getCode()==200){
                    toast("提交认证成功");
                    rlayout_shenhezhong.setVisibility(View.VISIBLE);
                }else {
                    toast(t.getMsg());
                }
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 0:
                if(data!=null){
                    school_id = data.getStringExtra("school_id");
                    tv_schoolname.setText(data.getStringExtra("SchoolName"));
                }
                break;
            case 1:
                if(data!=null){
                    img1 = data.getStringExtra("img1");
                    img2 =  data.getStringExtra("img2");
                    if(!TextUtils.isEmpty(img1)&&!TextUtils.isEmpty(img2)){
                        tv_photo.setText("已上传");
                    }else {
                        tv_photo.setHint("未完善");
                    }
                }
                break;
        }
    }

    /**
     * 时间初始化
     */
    private void initLunarPicker() {
        Calendar selectedDate = Calendar.getInstance();//系统当前时间
        Calendar startDate = Calendar.getInstance();
        Calendar endDate = Calendar.getInstance();

        startDate.set(Calendar.YEAR, endDate.get(Calendar.YEAR) - 70);

        pvCustomLunar = new TimePickerBuilder(IdentityActivity.this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                if (v instanceof TextView) {
                    TextView tv = (TextView) v;
                    tv.setText(getTime(date,"yyyy"));
                    ///EventBus.getDefault().post(new OrderRefreshByDateEntity(getTime(date,"yyyy-MM-dd")));
                }
            }
        }).setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setLayoutRes(R.layout.pickerview_custom_lunar, new CustomListener() {

                    @Override
                    public void customLayout(final View v) {
                        final TextView tvSubmit = v.findViewById(R.id.tv_finish);
                        TextView tvCancel = v.findViewById(R.id.tv_cancel);
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomLunar.returnData();
                                pvCustomLunar.dismiss();
                            }
                        });
                        tvCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomLunar.dismiss();
                            }
                        });
                    }
                })
                .setType(new boolean[]{true, false, false, false, false, false})
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setDividerColor(Color.RED)
                .build();
    }
    private String getTime(Date date,String fomat) {
        SimpleDateFormat format = new SimpleDateFormat(fomat);
        return format.format(date);
    }
}
